import os

import pygame

from common import (add_score, black, font_path, get_score, go_next_level, red,
                    white)

font = pygame.font.Font(font_path, 30)

# 设置窗口
screen_width, screen_height = 1200, 500

# 加载图片
images = []
image_directory = "./assets/game_6/"  # 图片文件夹路径
for i in range(5):
    image_path = os.path.join(image_directory, f"image-{i+1}.png")
    image = pygame.image.load(image_path)
    scale = (1200, 350) if i != 4 else (1000, 250)
    image = pygame.transform.scale(image, scale)
    images.append(image)

choices = []
for i in range(5):
    choice_path = os.path.join(image_directory, f"choices-{i+1}.png")
    choice = pygame.image.load(choice_path)
    scale = (1200, 150) if i != 4 else (1000, 130)
    choice = pygame.transform.scale(choice, (1200, 150))
    choices.append(choice)

cur_choice = 0

choices_rect = []
for i in range(4):
    rect = pygame.Rect(i * 300, 350, 300, 150)
    choices_rect.append(rect)


prompts = [
    "示自动闭塞区段，列车间隔五个闭塞分区，在绿灯下运行",
    "自动闭塞区段，列车间隔四个闭塞分区，在绿黄灯下运行",
    "自动闭塞区段，列车间隔三个闭塞分区，在黄灯下运行",
    "显示自动闭塞区段，列车接近车站时",
    "自动闭塞区段，车站同方向发车",
]
questions = [
    "请选择正确的列车追踪间隔时间",
    "请选择正确的列车运行间隔时间",
    "请选择正确的列车运行间隔时间",
    "请选择正确的列车接近车站的间隔时间",
    "请选择正确的车站同方向发车的间隔时间",
]
asnwers = [
    0,  # A
    2,  # C
    2,  # C
    3,  # D
    1,  # B
]

cur_level = 0


# 游戏主循环
def draw(screen):
    global running, cur_level

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos
            for i, rect in enumerate(choices_rect):
                if rect.collidepoint(x, y):
                    cur_choice = i
                    if cur_choice == asnwers[cur_level]:
                        if cur_level >= 4:
                            go_next_level()

                        else:
                            cur_level += 1
                    else:
                        add_score(-10)

    screen.fill(white)

    # draw prompt
    prompt = font.render(prompts[cur_level], True, black)
    screen.blit(prompt, (10, 10))

    # draw score at top right
    cur_score = get_score()
    score_text = font.render(f"当前得分：{cur_score}", True, red)
    screen.blit(score_text, (1000, 10))

    # draw image
    cur_image = images[cur_level]
    screen.blit(cur_image, (0, 60))
    # draw question
    question = font.render(questions[cur_level], True, black)
    screen.blit(question, (10, 320))

    # draw choice
    cur_choice_image = choices[cur_level]
    screen.blit(cur_choice_image, (0, 350))

    pygame.display.flip()
